﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BTSWMILibrary;
using System.Management;
using Microsoft.BizTalk.ExplorerOM;

namespace BizTalkConsoleObjects
{
    public class BizTalkWebConsoleOM
    {
        //Overriden in ASP.NET project and assinged from Web.config
        static string BizTalkMgmtDBName = "BizTalkMgmtDB";

        static string ServerName = ".";

        public static void SetConnections(string BizTalkMgmtDB, string serverName)
        {
            BizTalkMgmtDBName = BizTalkMgmtDB;
            ServerName = serverName;
            BTSWMILibrary.BizTalkWebConsoleWMIQueries.SetConnections(BizTalkMgmtDB);
        }

        public static GroupNameAndMgmtDb GetServerAndManagementDbName()
        {
            MSBTS_GroupSetting groupSettings = new MSBTS_GroupSetting();
            GroupNameAndMgmtDb groupNameMgmtDbObj = new GroupNameAndMgmtDb();

            groupSettings = BTSWMILibrary.BizTalkWebConsoleWMIQueries.GetServerAndManagementDbName();

            groupNameMgmtDbObj.MgmtDb = groupSettings.MgmtDbName;
            groupNameMgmtDbObj.MachineName = groupSettings.MgmtDbServerName;

            return groupNameMgmtDbObj;
        }

        public static List<Hosts> GetHosts()
        {
            List<MSBTS_Host> btshostList = BTSWMILibrary.BizTalkWebConsoleWMIQueries.GetHosts();
            List<Hosts> consoleHostList = new List<Hosts>();
            Hosts host = null;

            foreach (MSBTS_Host msbtsHost in btshostList)
            {
                host = new Hosts();
                host.Name = msbtsHost.Name;
                if (msbtsHost.HostType == "1")
                {
                    host.HostType = "In-Process";
                }
                else if (msbtsHost.HostType == "2")
                {
                    host.HostType = "Isolated";
                }
                host.WindowsGroup = msbtsHost.NTGroupName;
                host.Tracking = msbtsHost.HostTracking;
                host.Clustered = msbtsHost.ClusterResourceGroupName;
                consoleHostList.Add(host);
            }

            return consoleHostList;
        }

        public static List<HostInstances> GetHostInstances()
        {
            List<HostInstances> consoleHostInstances = new List<HostInstances>();
            List<BTSWMILibrary.MSBTS_HostInstance> wmiHostInstances = BTSWMILibrary.BizTalkWebConsoleWMIQueries.GetHostInstances();

            HostInstances consoleHostInstance = null;


            foreach (var wmiHostInstance in wmiHostInstances)
            {
                consoleHostInstance = new HostInstances();
                consoleHostInstance.HostName = wmiHostInstance.HostName;
                if (wmiHostInstance.HostType == "1")
                {
                    consoleHostInstance.HostType = "In-Process";
                    if (wmiHostInstance.ServiceState == "4")
                    {
                        consoleHostInstance.Status = "Running";
                    }
                    else if (wmiHostInstance.ServiceState == "1")
                    {
                        consoleHostInstance.Status = "Stopped";
                    }
                }
                else
                {
                    consoleHostInstance.HostType = "Isolated";
                    consoleHostInstance.Status = "Not Applicabe";
                }

                consoleHostInstance.isDisabled = wmiHostInstance.IsDisabled;
                consoleHostInstance.Logon = wmiHostInstance.Logon;
                consoleHostInstance.ServerName = wmiHostInstance.RunningServer;


                consoleHostInstances.Add(consoleHostInstance);
            }

            return consoleHostInstances;
        }

        public static List<Servers> GetServerName()
        {
            List<Servers> serverList = new List<Servers>();

            MSBTS_MsgBoxSetting msbtsMsgBoxSetting = BTSWMILibrary.BizTalkWebConsoleWMIQueries.GetMsgBoxSettings();
            Servers server = new Servers();
            server.Name = msbtsMsgBoxSetting.MsgBoxDBServerName;
            serverList.Add(server);
            return serverList;
        }

        public static List<MessageBoxes> GetMessageBoxSetting()
        {
            List<MessageBoxes> messageBoxes = new List<MessageBoxes>();
            MSBTS_MsgBoxSetting msbtsMsgBoxSetting = BTSWMILibrary.BizTalkWebConsoleWMIQueries.GetMsgBoxSettings();
            MessageBoxes msgBoxes = new MessageBoxes();
            msgBoxes.Database = msbtsMsgBoxSetting.MsgBoxDBName;
            msgBoxes.Server = msbtsMsgBoxSetting.MsgBoxDBServerName;
            msgBoxes.Master = msbtsMsgBoxSetting.IsMasterMsgBox;
            msgBoxes.Disabled = msbtsMsgBoxSetting.IsMasterMsgBox;
            messageBoxes.Add(msgBoxes);
            return messageBoxes;
        }

        public static List<BizTalkConsoleObjects.Applications> GetApplications()
        {
            BizTalkConsoleObjects.Applications BCOApplication = null;
            List<BizTalkConsoleObjects.Applications> BCOApplicationList = new List<Applications>();
            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.ApplicationCollection applicationCollection = catalog.Applications;
                foreach (Application applications in applicationCollection)
                {
                    BCOApplication = new Applications();
                    BCOApplication.Name = applications.Name;
                    BCOApplicationList.Add(BCOApplication);
                }

                // Commit the changes
                catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }


            return BCOApplicationList;
        }

        public static List<BizTalkConsoleObjects.Resources> GetResources(string ApplicationName)
        {
            List<BizTalkConsoleObjects.Resources> resources = new List<Resources>();
            BizTalkConsoleObjects.Resources resource = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.BtsAssemblyCollection assembliesCollection = catalog.Applications[ApplicationName].Assemblies;
                foreach (Microsoft.BizTalk.ExplorerOM.BtsAssembly assembly in assembliesCollection)
                {
                    //assembly.Application.BackReferences[0].Assemblies[0].DisplayName;
                    resource = new BizTalkConsoleObjects.Resources();
                    resource.Name = assembly.DisplayName;
                    resource.Application = assembly.Application.Name;
                    resources.Add(resource);
                    /*
                    Microsoft.BizTalk.ExplorerOM.PipelineCollection pipelinesCollection = catalog.Applications[ApplicationName].Pipelines;

                    foreach (Microsoft.BizTalk.ExplorerOM.Pipeline pipeline in pipelinesCollection)
                    {
                        //assembly.Application.BackReferences[0].Assemblies[0].DisplayName;
                        resource = new BizTalkConsoleObjects.Resources();
                        resource.Name = pipeline.BtsAssembly.DisplayName;
                        resource.Application = pipeline.Application.Name;
                        resources.Add(resource);
                    }*/
                }



                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return resources;
        }

        public static List<BizTalkConsoleObjects.Pipelines> GetPipelines(string ApplicationName)
        {
            List<BizTalkConsoleObjects.Pipelines> pipelines = new List<Pipelines>();
            BizTalkConsoleObjects.Pipelines pipeline = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.PipelineCollection pipelinesCollection = catalog.Applications[ApplicationName].Pipelines;

                foreach (Microsoft.BizTalk.ExplorerOM.Pipeline OMpipeline in pipelinesCollection)
                {
                    //assembly.Application.BackReferences[0].Assemblies[0].DisplayName;
                    pipeline = new BizTalkConsoleObjects.Pipelines();
                    pipeline.Name = OMpipeline.FullName;

                    switch (OMpipeline.Type)
                    {
                        case PipelineType.Receive:
                            pipeline.Type = "Receive";
                            break;
                        case PipelineType.Send:
                            pipeline.Type = "Send";
                            break;
                        case PipelineType.Transform:
                            pipeline.Type = "Transform";
                            break;
                        case PipelineType.Unknown:
                            pipeline.Type = "Unknown";
                            break;
                    }

                    pipeline.Assembly = OMpipeline.AssemblyQualifiedName;
                    pipeline.Application = OMpipeline.Application.Name;
                    pipelines.Add(pipeline);
                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return pipelines;
        }

        public static List<BizTalkConsoleObjects.Maps> GetMaps(string ApplicationName)
        {
            List<BizTalkConsoleObjects.Maps> maps = new List<Maps>();
            BizTalkConsoleObjects.Maps map = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.TransformCollection mapsCollection = catalog.Applications[ApplicationName].Transforms;

                foreach (Microsoft.BizTalk.ExplorerOM.Transform OMmaps in mapsCollection)
                {
                    map = new BizTalkConsoleObjects.Maps();
                    map.Name = OMmaps.FullName;
                    map.Assembly = OMmaps.AssemblyQualifiedName;
                    map.SourceSchema = OMmaps.SourceSchema.FullName;
                    map.TargetSchema = OMmaps.TargetSchema.FullName;
                    map.Application = OMmaps.Application.Name;
                    maps.Add(map);
                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return maps;
        }

        public static List<BizTalkConsoleObjects.Schemas> GetSchemas(string ApplicationName)
        {
            List<BizTalkConsoleObjects.Schemas> schemas = new List<Schemas>();
            BizTalkConsoleObjects.Schemas schema = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.SchemaCollection SchemasCollection = catalog.Applications[ApplicationName].Schemas;

                foreach (Microsoft.BizTalk.ExplorerOM.Schema OMSchemas in SchemasCollection)
                {
                    schema = new BizTalkConsoleObjects.Schemas();
                    schema.Name = OMSchemas.FullName;
                    schema.Assembly = OMSchemas.AssemblyQualifiedName;
                    schema.RootName = OMSchemas.RootName;
                    switch (OMSchemas.Type)
                    {
                        case SchemaType.Document:
                            schema.SchemaType = "Document";
                            break;
                        case SchemaType.Property:
                            schema.SchemaType = "Property";
                            break;
                    }

                    schema.TargetNamespace = OMSchemas.TargetNameSpace;
                    schema.Application = OMSchemas.Application.Name;
                    schemas.Add(schema);
                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return schemas;
        }

        public static List<BizTalkConsoleObjects.Policies> GetPolicies(string ApplicationName)
        {

            List<BizTalkConsoleObjects.Policies> policies = new List<Policies>();
            BizTalkConsoleObjects.Policies policy = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.PolicyCollection PolicyCollection = catalog.Applications[ApplicationName].Policies;

                foreach (Microsoft.BizTalk.ExplorerOM.Policy OMPolicy in PolicyCollection)
                {
                    policy = new BizTalkConsoleObjects.Policies();
                    policy.Name = OMPolicy.Name;
                    policy.Version = OMPolicy.MajorRevision + "." + OMPolicy.MinorRevision;
                    policy.Description = "";
                    policy.Application = ApplicationName;

                    switch (OMPolicy.Deployed)
                    {
                        case true:
                            policy.State = "Deployed";
                            break;
                        case false:
                            policy.State = "Published";
                            break;
                    }

                    policies.Add(policy);
                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return policies;

        }

        public static List<BizTalkConsoleObjects.ReceiveLocations> GetReceiveLocations(string ApplicationName)
        {

            List<BizTalkConsoleObjects.ReceiveLocations> receiveLocations = new List<ReceiveLocations>();
            BizTalkConsoleObjects.ReceiveLocations receiveLocation = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.ReceivePortCollection receivePortCollection = catalog.Applications[ApplicationName].ReceivePorts;

                foreach (Microsoft.BizTalk.ExplorerOM.ReceivePort receivePort in receivePortCollection)
                {
                    Microsoft.BizTalk.ExplorerOM.ReceiveLocationCollection receiveLocationCollection = receivePort.ReceiveLocations;

                    foreach (Microsoft.BizTalk.ExplorerOM.ReceiveLocation OMReceiveLocation in receiveLocationCollection)
                    {
                        receiveLocation = new BizTalkConsoleObjects.ReceiveLocations();
                        receiveLocation.Name = OMReceiveLocation.Name;

                        receiveLocation.URI = OMReceiveLocation.Address;
                        receiveLocation.TransportType = OMReceiveLocation.TransportType.Name;
                        receiveLocation.ReceivePort = receivePort.Name;
                        receiveLocation.ReceiveHandler = OMReceiveLocation.ReceiveHandler.Name;
                        receiveLocation.Application = ApplicationName;

                        switch (OMReceiveLocation.Enable)
                        {
                            case true:
                                receiveLocation.Status = "Enabled";
                                break;
                            case false:
                                receiveLocation.Status = "Disabled";
                                break;
                        }

                        receiveLocations.Add(receiveLocation);
                    }
                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return receiveLocations;

        }

        public static List<BizTalkConsoleObjects.ReceivePorts> GetReceivePorts(string ApplicationName)
        {

            List<BizTalkConsoleObjects.ReceivePorts> receivePorts = new List<ReceivePorts>();
            BizTalkConsoleObjects.ReceivePorts receivePort = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.ReceivePortCollection receivePortCollection = catalog.Applications[ApplicationName].ReceivePorts;

                foreach (Microsoft.BizTalk.ExplorerOM.ReceivePort OMreceivePort in receivePortCollection)
                {
                    receivePort = new ReceivePorts();

                    receivePort.Name = OMreceivePort.Name;

                    switch (OMreceivePort.IsTwoWay)
                    {
                        case true:
                            receivePort.TwoWay = "Yes";
                            break;

                        case false:
                            receivePort.TwoWay = "No";
                            break;
                    }

                    receivePort.Application = OMreceivePort.Application.Name;
                    receivePorts.Add(receivePort);

                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return receivePorts;

        }

        public static List<BizTalkConsoleObjects.SendPorts> GetSendPorts(string ApplicationName)
        {

            List<BizTalkConsoleObjects.SendPorts> sendPorts = new List<SendPorts>();
            BizTalkConsoleObjects.SendPorts sendPort = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.SendPortCollection sendPortCollection = catalog.Applications[ApplicationName].SendPorts;

                foreach (Microsoft.BizTalk.ExplorerOM.SendPort OMsendPort in sendPortCollection)
                {
                    sendPort = new SendPorts();

                    sendPort.Name = OMsendPort.Name;
                    if (OMsendPort.PrimaryTransport != null)
                    {
                        sendPort.TransportType = OMsendPort.PrimaryTransport.TransportType.Name;
                        sendPort.Handler = OMsendPort.PrimaryTransport.SendHandler.Name;
                        sendPort.URI = OMsendPort.PrimaryTransport.Address;
                    }
                    sendPort.Application = OMsendPort.Application.Name;

                    switch (OMsendPort.Status)
                    {
                        case PortStatus.Bound:
                            sendPort.Status = "Unenlisted";
                            break;

                        case PortStatus.Started:
                            sendPort.Status = "Started";
                            break;

                        case PortStatus.Stopped:
                            sendPort.Status = "Stopped";
                            break;

                    }

                    sendPorts.Add(sendPort);

                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return sendPorts;

        }

        public static List<BizTalkConsoleObjects.SendPortGroups> GetSendPortGroups(string ApplicationName)
        {

            List<BizTalkConsoleObjects.SendPortGroups> sendPortGroups = new List<SendPortGroups>();
            BizTalkConsoleObjects.SendPortGroups sendPortGroup = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.SendPortGroupCollection sendPortGroupCollection = catalog.Applications[ApplicationName].SendPortGroups;

                foreach (Microsoft.BizTalk.ExplorerOM.SendPortGroup OMsendPortGroup in sendPortGroupCollection)
                {
                    sendPortGroup = new SendPortGroups();

                    sendPortGroup.Name = OMsendPortGroup.Name;

                    switch (OMsendPortGroup.Status)
                    {
                        case PortStatus.Bound:
                            sendPortGroup.Status = "Unenlisted";
                            break;

                        case PortStatus.Started:
                            sendPortGroup.Status = "Started";
                            break;

                        case PortStatus.Stopped:
                            sendPortGroup.Status = "Stopped";
                            break;
                    }

                    sendPortGroup.Application = OMsendPortGroup.Application.Name;

                    sendPortGroups.Add(sendPortGroup);

                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return sendPortGroups;

        }

        public static List<BizTalkConsoleObjects.RoleLinks> GetRoleLinks(string ApplicationName)
        {

            List<BizTalkConsoleObjects.RoleLinks> roleLinks = new List<RoleLinks>();
            BizTalkConsoleObjects.RoleLinks roleLink = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.RoleCollection roleLinkCollection = catalog.Applications[ApplicationName].Roles;

                foreach (Microsoft.BizTalk.ExplorerOM.Role OMroles in roleLinkCollection)
                {
                    roleLink = new RoleLinks();

                    roleLink.Name = OMroles.Name;
                    roleLink.Assembly = OMroles.BtsAssembly.Name;
                    roleLink.LinkType = OMroles.ServiceLinkType;
                    roleLink.Application = OMroles.Application.Name;

                    //Should be implemented (later)
                    //roleLink.Orchestration;
                    //roleLink.Association;

                    roleLinks.Add(roleLink);

                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return roleLinks;

        }

        public static List<BizTalkConsoleObjects.Orchestrations> GetOrchestrations(string ApplicationName)
        {

            List<BizTalkConsoleObjects.Orchestrations> orchestrations = new List<Orchestrations>();
            BizTalkConsoleObjects.Orchestrations orchestration = null;

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                Microsoft.BizTalk.ExplorerOM.BtsOrchestrationCollection orchestrationCollection = catalog.Applications[ApplicationName].Orchestrations;

                foreach (Microsoft.BizTalk.ExplorerOM.BtsOrchestration OMOrchestration in orchestrationCollection)
                {
                    orchestration = new Orchestrations();

                    orchestration.Name = OMOrchestration.FullName;
                    orchestration.Assembly = OMOrchestration.BtsAssembly.Name;

                    if (OMOrchestration.Host != null)
                    {
                        orchestration.Host = OMOrchestration.Host.Name;

                        switch (OMOrchestration.Status)
                        {
                            case OrchestrationStatus.Enlisted:
                                orchestration.Status = "Stopped";
                                break;

                            case OrchestrationStatus.Started:
                                orchestration.Status = "Started";
                                break;

                            case OrchestrationStatus.Unenlisted:
                                orchestration.Status = "Unenlisted";
                                break;
                        }
                    }
                    else
                    {
                        orchestration.Status = "Unenlisted (unbound)";
                    }


                    orchestration.Application = OMOrchestration.Application.Name;

                    orchestrations.Add(orchestration);

                }

                // Commit the changes
                //catalog.SaveChanges();
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

            return orchestrations;

        }

        public static List<BizTalkConsoleObjects.Adapters> GetAdapters()
        {
            List<BTSWMILibrary.MSBTS_AdapterSetting> adapterSettingsWMI = BTSWMILibrary.BizTalkWebConsoleWMIQueries.GetAdapterSettings();

            List<BizTalkConsoleObjects.Adapters> adapterSettings = new List<Adapters>();

            BizTalkConsoleObjects.Adapters adapterObj;

            foreach (BTSWMILibrary.MSBTS_AdapterSetting adapter in adapterSettingsWMI)
            {
                adapterObj = new Adapters();
                adapterObj.Name = adapter.Name;
                adapterSettings.Add(adapterObj);
            }

            return adapterSettings;
        }

        #region Queries Used for Actions

        public static string GetHostInstanceStatus(string HostInstance, string ServerName)
        {
            ManagementObject classInstance =
                    new ManagementObject("root\\MicrosoftBizTalkServer",
                    "MSBTS_HostInstance.MgmtDbNameOverride='',MgmtDbServerOverride='',Name='Microsoft BizTalk Server BizTalkServerApplication " + ServerName + "'",
                    null);

            // no method in-parameters to define


            // Execute the method and obtain the return values.
            ManagementBaseObject outParams =
                classInstance.InvokeMethod("GetState", null, null);

            string status = "Not Applicable";

            if (outParams["State"].ToString() == "1")
            {
                status = "Stopped";
            }
            else if (outParams["State"].ToString() == "4")
            {
                status = "Running";
            }

            return status;
        }

        public static OrchestrationStatus GetOrchestrationStatus(string Orchestration, string ApplicationName)
        {
          
            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                return catalog.Applications[ApplicationName].Orchestrations[Orchestration].Status;

            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

        }

        public static bool GetReceiveLocationStatus(string ReceiveLocation, string ReceivePort, string ApplicationName)
        {
            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
            try
            {
                return catalog.Applications[ApplicationName].ReceivePorts[ReceivePort].ReceiveLocations[ReceiveLocation].Enable;

           }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }
        }

        public static Microsoft.BizTalk.ExplorerOM.PortStatus GetSendPortStatus(string SendPort, string ApplicationName)
        {
            
            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);
           
            try
            {
                return catalog.Applications[ApplicationName].SendPorts[SendPort].Status;
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

        }

        public static Microsoft.BizTalk.ExplorerOM.PortStatus GetSendPortGroupsStatus(string SendPortGroups, string ApplicationName)
        {

            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = string.Format("SERVER={0};DATABASE={1};Integrated Security=SSPI", ServerName, BizTalkMgmtDBName);

            try
            {
                return catalog.Applications[ApplicationName].SendPortGroups[SendPortGroups].Status;
            }
            catch (Exception e)
            {
                catalog.DiscardChanges();
                throw e;
            }

        }

        #endregion
    }
}
